using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Navigant.Infrastructure.Helpers;

namespace Navigant.Web.UI
{
	public abstract class ListControl : WebControl
    {
        #region Fields

        private int contentHeight;
		private int gridCellIndex;
		private List<string> spaces;
		private HtmlTable layoutTable;
		private HtmlTableCell titleCell;
		private HtmlTableRow headerRow;
		private HtmlTable gridTable;
		private HtmlTableRow currentGridRow;

        #endregion

        #region Properties

        protected string Title
		{
			get { return this.titleCell.InnerText; }
			set { this.titleCell.InnerText = value; }
		}

		protected int ContentHeight
		{
			get { return this.contentHeight; }
			set { this.contentHeight = value; }
		}

		protected string GridDivClientID
		{
			get { return this.gridTable.Parent.ClientID; }
		}

        #endregion

        #region OnInit

        protected override void OnInit(EventArgs e)
		{
			try
			{
				base.OnInit(e);

                this.spaces = new List<string>();

				this.layoutTable = new HtmlTable();
				this.layoutTable.CellSpacing = 0;
				this.layoutTable.CellPadding = 0;
				this.layoutTable.Border = 0;
				this.layoutTable.Width = "100%";
				this.Controls.Add(this.layoutTable);

				HtmlTableRow row = new HtmlTableRow();
				this.layoutTable.Rows.Add(row);

				this.titleCell = new HtmlTableCell();
				this.titleCell.Attributes.Add("class", "ms-WPHeader");
				this.titleCell.Style.Add("border-left", "1px solid #e4e4e4");
				this.titleCell.Style.Add("border-right", "1px solid #e4e4e4");
				this.titleCell.Style.Add("border-top", "1px solid #e4e4e4");
				this.titleCell.Style.Add("font-weight", "bold");
				this.titleCell.Style.Add("padding", "2px");
				this.titleCell.Style.Add("white-space", "nowrap");
				row.Cells.Add(this.titleCell);

				row = new HtmlTableRow();
				layoutTable.Rows.Add(row);

				HtmlTableCell cell = new HtmlTableCell();
				row.Cells.Add(cell);

				HtmlGenericControl containerDiv;

				if (this.contentHeight > 0)
				{
					containerDiv = new ScrollableDiv();
					containerDiv.Style.Add("height", this.contentHeight.ToString());
				}
				else
				{
					containerDiv = new HtmlGenericControl("div");
				}

				containerDiv.Style.Add("border", "1px solid #e4e4e4");
				cell.Controls.Add(containerDiv);

				this.gridTable = new HtmlTable();
				this.gridTable.CellSpacing = 0;
				this.gridTable.CellPadding = 2;
				this.gridTable.Border = 0;
				this.gridTable.Width = "100%";
				containerDiv.Controls.Add(this.gridTable);

				this.headerRow = new HtmlTableRow();
				this.gridTable.Rows.Add(this.headerRow);
			}
			catch (Exception ex)
			{
				Logger.LogError(ex);
			}
		}

        #endregion

        #region AddHeaderCell

        protected void AddHeaderCell(string text, int spaceAfter)
		{
			HtmlTableCell headerCell = this.CreateHeaderCell(spaceAfter);
			headerCell.InnerText = text;
		}

        #endregion

        #region CreateHeaderCell

        protected HtmlTableCell CreateHeaderCell(int spaceAfter)
		{
			HtmlTableCell headerCell = new HtmlTableCell();
			headerCell.Style.Add("font-weight", "bold");
			headerCell.Style.Add("white-space", "nowrap");
			this.headerRow.Cells.Add(headerCell);

			HtmlTableCell spacerCell = new HtmlTableCell();
			spacerCell.InnerHtml = "&nbsp";
			spacerCell.Width = spaceAfter.ToString();
			this.headerRow.Cells.Add(spacerCell);

			this.spaces.Add(spaceAfter.ToString());

			return headerCell;
		}

        #endregion

        #region AddGridRow

        protected void AddGridRow()
		{
			this.gridCellIndex = 0;

			this.currentGridRow = new HtmlTableRow();
			this.gridTable.Rows.Add(this.currentGridRow);
		}

        #endregion

        #region AddGridCell

        protected void AddGridCell(string text)
		{
            HtmlTableCell gridCell = this.CreateGridCell();
			
			if (text != "") gridCell.InnerText = text;
			else gridCell.InnerHtml = "&nbsp;";
		}

        #endregion

        #region CreateGridCell

        protected HtmlTableCell CreateGridCell()
		{
			HtmlTableCell gridCell = new HtmlTableCell();
			gridCell.Style.Add("border-top", "1px solid #e4e4e4");
			gridCell.Style.Add("white-space", "nowrap");
			this.currentGridRow.Cells.Add(gridCell);

			HtmlTableCell spacerCell = new HtmlTableCell();
			spacerCell.InnerHtml = "&nbsp";
			spacerCell.Width = (string)this.spaces[this.gridCellIndex];
			spacerCell.Style.Add("border-top", "1px solid #e4e4e4");
			this.currentGridRow.Cells.Add(spacerCell);

			this.gridCellIndex ++;

			return gridCell;
		}

        #endregion

        #region CreateRow

        protected HtmlTableRow CreateRow()
		{
			HtmlTableRow row = new HtmlTableRow();
			this.layoutTable.Rows.Add(row);

			return row;
		}

        #endregion

        #region CreateFullRowGridCell

        protected HtmlTableCell CreateFullRowGridCell(bool insertAtTop)
		{
			this.currentGridRow = new HtmlTableRow();

			if (insertAtTop)
				this.gridTable.Rows.Insert(0, this.currentGridRow);
			else
				this.gridTable.Rows.Add(this.currentGridRow);

			this.gridCellIndex = this.spaces.Count - 1;

			HtmlTableCell gridCell = this.CreateGridCell();
			gridCell.EnableViewState = false;
			gridCell.ColSpan = 2 * this.spaces.Count - 1;

			if (insertAtTop)
			{
				foreach (HtmlTableCell cell in this.currentGridRow.Cells)
				{
					cell.Style.Remove("border-top");
					cell.Style.Add("border-bottom", "1px solid #e4e4e4");
				}
			}

			return gridCell;
		}

        #endregion

        #region ResetScroll

        protected void ResetScroll()
		{
			ScrollableDiv scrollableDiv = this.gridTable.Parent as ScrollableDiv;
            if (scrollableDiv != null)
            {
                scrollableDiv.ResetScroll();
            }
		}

        #endregion

        #region DisplayMessage

        protected void DisplayMessage(string message)
		{
			this.AddGridRow();
			this.gridCellIndex = this.spaces.Count - 1;
			HtmlTableCell gridCell = this.CreateGridCell();
			gridCell.EnableViewState = false;
			gridCell.ColSpan = 2 * this.spaces.Count - 1;
			gridCell.Style.Remove("white-space");
			gridCell.InnerText = message;
        }

        #endregion
    }
}
